package hhm.android.my

import android.os.Bundle
import android.view.View
import androidx.databinding.DataBindingUtil
import hhm.android.library.utils.OnMultiClickListener
import hhm.android.my.databinding.ActivityClassControlBinding
import siau.android.base.HttpHelper
import siau.android.base.SBApplication
import siau.android.base.SBBaseActivity
import siau.android.base.enum.HeightClass
import siau.android.base.enum.WeightClass
import siau.android.http.model.SwapUnitRequest

class ClassControlActivity : SBBaseActivity() {

    lateinit var dataBinding: ActivityClassControlBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setTitleWhiteAndTextBlank()
        dataBinding = DataBindingUtil.setContentView(this, R.layout.activity_class_control)
        initTitle(getString(R.string.class_switch))

//        ring_main_5
//        oval_white
        when (SBApplication.userData.weightUnit) {
            WeightClass.KG.msg -> {
                dataBinding.activityClassControlKgIv.setImageResource(R.drawable.ring_main_5)
                dataBinding.activityClassControlJinIv.setImageResource(R.drawable.oval_white)
                dataBinding.activityClassControlBangIv.setImageResource(R.drawable.oval_white)
            }
            WeightClass.JIN.msg -> {
                dataBinding.activityClassControlKgIv.setImageResource(R.drawable.oval_white)
                dataBinding.activityClassControlJinIv.setImageResource(R.drawable.ring_main_5)
                dataBinding.activityClassControlBangIv.setImageResource(R.drawable.oval_white)
            }
            WeightClass.LB.msg -> {
                dataBinding.activityClassControlKgIv.setImageResource(R.drawable.oval_white)
                dataBinding.activityClassControlJinIv.setImageResource(R.drawable.oval_white)
                dataBinding.activityClassControlBangIv.setImageResource(R.drawable.ring_main_5)
            }
        }
        when (SBApplication.userData.heightUnit) {
            HeightClass.CM.msg -> {
                dataBinding.activityClassControlCmIv.setImageResource(R.drawable.ring_main_5)
                dataBinding.activityClassControlFtIv.setImageResource(R.drawable.oval_white)
            }
            HeightClass.FT.msg -> {
                dataBinding.activityClassControlCmIv.setImageResource(R.drawable.oval_white)
                dataBinding.activityClassControlFtIv.setImageResource(R.drawable.ring_main_5)
            }
        }


        dataBinding.activityClassControlKgLl.setOnClickListener(object : OnMultiClickListener() {
            override fun onMultiClick(v: View?) {
                if (SBApplication.userData.weightUnit == WeightClass.KG.msg) {
                    return
                }
                showLoadingFragment(R.id.activity_class_control_fl)
                val request = SwapUnitRequest(1, WeightClass.KG.msg)
                HttpHelper().swapUnit(request)?.subscribe({
                    SBApplication.userData.weightUnit = WeightClass.KG.msg
                    dataBinding.activityClassControlKgIv.setImageResource(R.drawable.ring_main_5)
                    dataBinding.activityClassControlJinIv.setImageResource(R.drawable.oval_white)
                    dataBinding.activityClassControlBangIv.setImageResource(R.drawable.oval_white)
                    removeLoadingFragment()
                }, {
                    showToast(it.message)
                    removeLoadingFragment()
                })
            }
        })
        dataBinding.activityClassControlJinLl.setOnClickListener(object : OnMultiClickListener() {
            override fun onMultiClick(v: View?) {
                if (SBApplication.userData.weightUnit == WeightClass.JIN.msg) {
                    return
                }
                showLoadingFragment(R.id.activity_class_control_fl)
                val request = SwapUnitRequest(1, WeightClass.JIN.msg)
                HttpHelper().swapUnit(request)?.subscribe({
                    SBApplication.userData.weightUnit = WeightClass.JIN.msg
                    dataBinding.activityClassControlKgIv.setImageResource(R.drawable.oval_white)
                    dataBinding.activityClassControlJinIv.setImageResource(R.drawable.ring_main_5)
                    dataBinding.activityClassControlBangIv.setImageResource(R.drawable.oval_white)
                    removeLoadingFragment()
                }, {
                    showToast(it.message)
                    removeLoadingFragment()
                })
            }
        })
        dataBinding.activityClassControlBangLl.setOnClickListener(object : OnMultiClickListener() {
            override fun onMultiClick(v: View?) {
                if (SBApplication.userData.weightUnit == WeightClass.LB.msg) {
                    return
                }
                showLoadingFragment(R.id.activity_class_control_fl)
                val request = SwapUnitRequest(1, WeightClass.LB.msg)
                HttpHelper().swapUnit(request)?.subscribe({
                    SBApplication.userData.weightUnit = WeightClass.LB.msg
                    dataBinding.activityClassControlKgIv.setImageResource(R.drawable.oval_white)
                    dataBinding.activityClassControlJinIv.setImageResource(R.drawable.oval_white)
                    dataBinding.activityClassControlBangIv.setImageResource(R.drawable.ring_main_5)
                    removeLoadingFragment()
                }, {
                    showToast(it.message)
                    removeLoadingFragment()
                })
            }
        })

        dataBinding.activityClassControlCmLl.setOnClickListener(object : OnMultiClickListener() {
            override fun onMultiClick(v: View?) {
                if (SBApplication.userData.heightUnit == HeightClass.CM.msg) {
                    return
                }
                showLoadingFragment(R.id.activity_class_control_fl)
                val request = SwapUnitRequest(2, HeightClass.CM.msg)
                HttpHelper().swapUnit(request)?.subscribe({
                    SBApplication.userData.heightUnit = HeightClass.CM.msg
                    dataBinding.activityClassControlCmIv.setImageResource(R.drawable.ring_main_5)
                    dataBinding.activityClassControlFtIv.setImageResource(R.drawable.oval_white)
                    removeLoadingFragment()
                }, {
                    showToast(it.message)
                    removeLoadingFragment()
                })
            }
        })
        dataBinding.activityClassControlFtLl.setOnClickListener(object : OnMultiClickListener() {
            override fun onMultiClick(v: View?) {
                if (SBApplication.userData.heightUnit == HeightClass.FT.msg) {
                    return
                }
                showLoadingFragment(R.id.activity_class_control_fl)
                val request = SwapUnitRequest(2, HeightClass.FT.msg)
                HttpHelper().swapUnit(request)?.subscribe({
                    SBApplication.userData.heightUnit = HeightClass.FT.msg
                    dataBinding.activityClassControlCmIv.setImageResource(R.drawable.oval_white)
                    dataBinding.activityClassControlFtIv.setImageResource(R.drawable.ring_main_5)
                    removeLoadingFragment()
                }, {
                    showToast(it.message)
                    removeLoadingFragment()
                })
            }
        })
    }
}